package getPoint;

import FFT.Fft;
import FFT.FingerPrint;
import fingerStore.DBOpener;
import fingerStore.ShazamHash;
import wavRead.WavFileReader;

import java.io.File;
import java.util.*;

/**
 * Created by 77430 on 2016/11/13.
 */
public class Audiodata {
    private static File[] AudioFileName() {
        System.out.println("请输入待测音频文件夹的路径");
        Scanner sc = new Scanner(System.in);
        while (true) {
            String s = sc.nextLine();
            File autioFileName = new File(s);
            if (autioFileName.isAbsolute()) {
                return autioFileName.listFiles();
            } else {
                System.out.println("请输入正确的文件名");
            }
        }
    }

    private static Object getMaxValue(Map<Short, Integer> map) {
        if (map == null) return null;
        Collection<Integer> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        return obj[obj.length - 1];
    }

    //K=5;
    private static Object[] getKMaxValue(Map<Short, Object> map, int k) {
        if (map == null) return null;
        Object[] kmaxValue = new Object[k];
        Collection<Object> c = map.values();
        Object[] obj = c.toArray();
        Arrays.sort(obj);
        for (int i = 0; i < kmaxValue.length; ++i) {
            kmaxValue[i] = obj[obj.length - 1 - i];
        }
        return kmaxValue;
    }

    private static Short getKey(Map<Short, Object> map, Object value) {
        for (Map.Entry<Short, Object> m : map.entrySet()
                ) {
            if (value.equals(m.getValue())) {
                Short key = m.getKey();
                map.remove(m.getKey());
                return key;
            }
        }
        return null;
    }

    private static Map<Short, Object> getScore(Map<Short, List<Short>> id_offSet) {
        Map<Short, Object> score = new HashMap<>();
        for (Map.Entry<Short, List<Short>> m :
                id_offSet.entrySet()) {
            Map<Short, Object> scores = new HashMap<>();
            for (Short b : m.getValue()
                    ) {
                if (scores.containsKey(b)) {
                    Object qty = scores.get(b);
                    scores.put(b, (Integer) qty + 1);
                } else
                    scores.put(b, 1);
            }
            Object []objs = getKMaxValue(scores,1);
            int _score = 0;
            for (Object obj : objs
                    ) {
                _score += (Integer) obj;
                score.put(m.getKey(),_score);
            }
        }
        return score;
    }
    public static void main(String[] args) {
        for (File file : AudioFileName()
                ) {
            WavFileReader wavFileReader = new WavFileReader(file);
            System.out.println(file.getName());
            System.out.println("fingerprint...........");
            double[] freqDomain = wavFileReader.getData();
            Object[] subAray = WavFileReader.splitAry(freqDomain, Fft.WINDOW_SIZE);
            FingerPrint newPrint = new FingerPrint();
            for (Object item : subAray
                    ) {
                double[] domain = (double[]) item;
                newPrint.append(domain);
            }
            ArrayList<ShazamHash> hashes = newPrint.combineHash();

            DBOpener opener = new DBOpener();
            Map<Short, List<Short>> id_offSet = new HashMap<>(30);
            Map<Short, Object> score;


            id_offSet = opener.getoffSet(id_offSet, hashes);

            score = getScore(id_offSet);

            Object[] maxScores = getKMaxValue(score, 5);
            for (int j = 0; j < maxScores.length; j++) {
                String name = opener.getName(getKey(score, maxScores[j]));
                System.out.println("排行榜第" + (j + 1) + "名" + "歌名:" + name + " " + "分数:" + maxScores[j]);
            }

            System.out.println("...............");
        }
    }
}
